<?php
/**
 * @Notes:【】
 * @Date: 2022-07-02 9:42
 */

namespace App\Service\Shipping;

use App\Dao\Shipping\ShippingTemplatesDao;
use App\Dao\Shipping\ShippingTemplatesRegionDao;
use App\Model\Shipping\ShippingTemplates;
use App\Model\Shipping\ShippingTemplatesRegion;
use App\Service\BaseService;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Utils\Arr;

class ShiippingTemplatesService extends BaseService
{

    /**
     * @Inject()
     * @var ShippingTemplatesRegionDao
     */
    protected $shippingTemplatesRegionDao;

    /**
     * @Inject()
     * @var ShippingTemplatesDao
     */
    protected $shippingTemplatesDao;

    public function lists($params)
    {
        $data = $this->shippingTemplatesDao->lists($params);
        _SUCCESS($data);
    }

    public function add(\App\Tool\Collect $params)
    {

        Db::beginTransaction();
        try {
            $shippingTemplatesDao = $this->shippingTemplatesDao->create($params->all());
            $type = $params->get('type');
            $this->updateRegion($params->get('region'), $shippingTemplatesDao->id, $type, $shippingTemplatesDao);
            Db::commit();
        } catch (\Exception $exception) {
            Db::rollBack();
            _Error($exception->getMessage());
        }
        return true;
    }

    public function updateRegion($region, $temp_id, $type, $shippingTemplatesDao)
    {
        $region = _Collect($region);
        $regionDatas = [];
        $region->every(function ($item) use (&$regionDatas, $temp_id, $type) {
            $uniqid = uniqid() . rand(1000, 9999);
            foreach ($item['areas_ids'] as $areas_ids) {
                $regionData['uniqid'] = $uniqid;
                $regionData['province_id'] = $areas_ids[0];
                $regionData['city_id'] = $areas_ids[1];
                $regionData['type'] = $type;
                $regionData = $regionData + Arr::only($item, ['first', 'first_price', 'continue', 'continue_price']);
                $regionDatas[] = $regionData;
            }
            return true;
        });
        $regionDatas = _Collect($regionDatas);
        $regionDatas->transform(function ($item) {
            return new ShippingTemplatesRegion($item);
        });
        $shippingTemplatesDao->ShippingTemplatesRegion()->forceDelete();
        $shippingTemplatesDao->ShippingTemplatesRegion()->saveMany($regionDatas->all());

    }

    public function detail(\App\Tool\Collect $params)
    {
        $data = $this->shippingTemplatesDao->detail($params->get('id'));
        return $data;
    }

    public function edit($id, \App\Tool\Collect $data)
    {
        Db::beginTransaction();
        try {
            $shippingTemplatesDao = $this->shippingTemplatesDao->where('id', $id)->firstOrError();
            $shippingTemplatesDao->fill($data->all());
            $shippingTemplatesDao->save();
            $this->updateRegion($data->get('shipping_templates_region'), $shippingTemplatesDao->id, $shippingTemplatesDao->type, $shippingTemplatesDao);
            Db::commit();
        } catch (\Exception $exception) {
            Db::rollBack();
            _Error($exception->getMessage());
        }

        return true;
    }
}